<?php
/**
 * 公开页面访问接口
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: PublicController.class.php v1.0 2014-06-12 17:27:03 $
 */
namespace Admin\Controller;

class PublicController extends CommonController {

    public function _initialize() {
        parent::_initialize();
         // 开启令牌
        if (ACTION_NAME == 'index') {
            C('TOKEN_ON', true);
        }

        // 需要登录才能访问的action
        $filterLogin = array('logout');
        if (in_array(ACTION_NAME, $filterLogin)) {
            $this->filterLogin();
        }

        // 登录后不可访问的action
        $filterAction = array('index', 'login');
        if (in_array(ACTION_NAME, $filterAction) && $this->hasLogin()) {
            return $this->redirect('Index/index');
        }
    }
    
    /**
     * 管理员登录页
     * @return
     */
    public function index() {
        layout(false);
        $this->display();
    }
    /**
     * 管理员登录
     * @return
     */
    public function login() {
        $reloadUrl = U('Public/index');
        $login_info = I("post.admin");
        $verify_code = I("post.verify_code");
        if (empty($login_info['username'])) {
            return $this->errorReturn('请填写登录用户名！', $reloadUrl);
        }

        if (empty($login_info['pwd'])) {
            return $this->errorReturn('请填写登录密码！', $reloadUrl);
        }

        if (!M('Admin')->autoCheckToken($_POST)) {
            return $this->errorReturn('登录令牌超时！', $reloadUrl);
        }

        if (!check_verify_code($verify_code)) {
            return $this->errorReturn('验证码不正确！', $reloadUrl);
        }

        $SysAdminUserModel = D('Admin', 'Service');
        
        // 登录认证
        $result = $SysAdminUserModel->login($login_info);

        if (!$result['status']) {
            return $this->errorReturn($result['data']['error'], $reloadUrl);
        }

        return $this->successReturn('登录成功！', U('Index/index'));
    }

    /**
     * 管理员登出
     * @return
     */
    public function logout() {
        D('Admin', 'Service')->logout();

        $this->success('登出成功！', U('Public/index'));
    }
    
    /**
     * 验证码图片
     * @return
     */
    public function verifyCode() {
        $config = array();
        $codetype = C("code_type");
        switch ($codetype){
            case 0:
                $config['codeSet'] = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
                break;
            case 1:
                $config['codeSet'] = '0123456789';
                break;
            case 2:
                $config['codeSet'] = 'abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
                break;
            case 3:
                $config['useZh'] = true; 
                //$config['zhSet'] = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这';
                break;
        }
        $config['useNoise'] = C("code_useNoise");
        $config['useImgBg'] = C("code_useImgBg");
        $config['useCurve'] = C("code_useCurve");
        $config['length'] = C("code_length");
        $config['fontSize'] = C("code_fontsize");
        //$config['imageW'] = C("code_width");
        //$config['imageH'] = C("code_height");
        
        //var_dump($config);die;
        create_verify_code($config);
    }
}
